package com.cosc645.ecash;

import java.util.Comparator;
import java.util.concurrent.PriorityBlockingQueue;


public class PriorityQueue {

    /**
     * Lowest number Priority popped first
     */
    public PriorityBlockingQueue<MoneyOrder> moneyOrderQueue;

    public PriorityQueue(int size)
    {
        moneyOrderQueue = new PriorityBlockingQueue<MoneyOrder>(size, new Comparator<MoneyOrder>() {
            @Override
            public int compare(MoneyOrder a, MoneyOrder b) {
                return (a.getPriority() < b.getPriority() ? -1 : (a.getPriority() == b.getPriority() ? 0 : 1));
            }
        });
    }

    //Removes the highest priority object from the queue and returns it as "Process" object.
    public MoneyOrder pop() {
        try {
            return moneyOrderQueue.take();
        } catch (InterruptedException e) { //if interrupted while waiting
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    public void add(MoneyOrder value) {
        moneyOrderQueue.put(value);
    }

    public boolean isEmpty() {
        return moneyOrderQueue.isEmpty();
    }

}
